﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Cosmetic.DataEntity;
using Cosmetic.Logic;

public partial class Management_Product_ProductList : PageBase2
{
    
	private ProductManager m_ProductManager			= new ProductManager();
	private CommentManager m_CommentManager			= new CommentManager();
	private BrandManager m_BrandManager				= new BrandManager();
	private ProductSeriesManager m_SeriesManager	= new ProductSeriesManager();
	private CategoryManager m_CategoryManager		= new CategoryManager();
    private IntegralManager m_IntegralManager       = new IntegralManager();

    private RoleManager m_RoleManager = new RoleManager();

    private Cosmetic.DataEntity.Category m_Category = new Category();
    private Cosmetic.DataEntity.Category SaveFrontCategory = new Category();

	protected void Page_Load(object sender, EventArgs e)
	{
        if (this.SignInUser1 == null)
        {
            Session.Remove("SignInUser1");
            this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('请先登录权限!');window.top.location.href='/Management/indexs.aspx';", true);
            return;
        }
        if (!IsPostBack)
        {
            Cosmetic.DataEntity.User theUser = this.SignInUser1;
            if (!this.m_RoleManager.CheckUserRole(theUser, RoleManager.ERole.Admin))
            {
                Session.Remove("SignInUser1");
                this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('你没有管理员权限!');window.top.location.href='/Management/indexs.aspx';", true);
                return;
            }
        }

		this.ddlBrand.SelectedBrandChanged		+= new Controls_BrandDropDownList.SelectedBrandChangedHandler(this.ddlSeries.BrandChanged);		
		
	}

    private void FillDataToddlL1Category()
    {
        List<Category> listCategory = m_CategoryManager.GetSubCateogoriesBySupCategory(m_CategoryManager.GetCategoryByName(CategoryManager.ETopCategory.Category_L.ToString()));
        BindDataToDropDownList(ddlL1Category, listCategory);
    }


    private void BindDataToDropDownList(DropDownList aDropDownList, List<Category> aCategoryList)
    {
        aDropDownList.Items.Clear();

        if (aCategoryList.Count > 0)
        {
            aDropDownList.DataSource = aCategoryList;
            aDropDownList.DataTextField = "Name";
            aDropDownList.DataValueField = "CategoryId";
            aDropDownList.DataBind();
            aDropDownList.Items.Insert(0, new ListItem(""));
            aDropDownList.Enabled = true;
        }
        else
        {
            aDropDownList.Enabled = false;
        }
    }

    protected void ddlL1Category_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(ddlL1Category.SelectedValue))
        {
            List<Category> listCategory = m_CategoryManager.GetCategoryById_L(new Guid(ddlL1Category.SelectedValue.ToString()));
            BindDataToDropDownList(ddlL2Category, listCategory);
        }
        else
        {
            ddlL2Category.Items.Clear();
           
        }
    }

    protected void ddlL2Category_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(ddlL2Category.SelectedValue))
        {
            List<Category> listCategory = m_CategoryManager.GetCategoryById_L(new Guid(ddlL2Category.SelectedValue.ToString()));
            BindDataToDropDownList(ddlL3Category, listCategory);
        }
        else
        {
            ddlL3Category.Items.Clear();
        }
        
    }


    protected void ddlL3Category_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(ddlL3Category.SelectedValue))
        {
            List<Category> listCategory = m_CategoryManager.GetCategoryById_L(new Guid(ddlL3Category.SelectedValue.ToString()));

        }

    }
	protected void Page_LoadComplete(object sender, EventArgs e)
	{
		if (!IsPostBack)
		{
           
            FillDataToddlL1Category();

		
			string theCategoryId	= this.Request.QueryString["CID"];
            //if (!String.IsNullOrEmpty(theBrandId))
            //{
            //    this.ddlBrand.SelectedBrand		= this.m_BrandManager.GetBrandById(new Guid(theBrandId));

            //    this.rblType.SelectedIndex		= 0;
            //}
            //else if (!String.IsNullOrEmpty(theSeriesId))
            //{
            //    ProductSeries theSeries			= this.m_SeriesManager.GetProductSeriesById(new Guid(theSeriesId));
            //    this.ddlBrand.SelectedBrand		= theSeries.Brand;
            //    this.ddlSeries.SelectedSeries	= theSeries;

            //    this.rblType.SelectedIndex		= 0;
            //}
			if (!String.IsNullOrEmpty(theCategoryId))
			{
				
				this.rblType.SelectedIndex		= 1;
			}
			else
			{
				this.rblType.SelectedIndex		= 0;
			}

			this.rblType_SelectedIndexChanged(null, null);
            if (!string.IsNullOrEmpty(this.Request.QueryString["CID"]))
            {
                lvProdust.DataSource = this.m_ProductManager.GetProductByCategory(new Guid(this.Request.QueryString["CID"]));
                lvProdust.DataBind();
            }
			
		}
	}



	protected void Page_PreRender(object sender, EventArgs e)
    {
		BindProduct();
    }


	protected void lvProdust_ItemCommand(object sender, ListViewCommandEventArgs e)
	{
		if (e.Item.ItemType == ListViewItemType.DataItem)
		{
			Product theProduct	= this.m_ProductManager.GetProductById(new Guid(e.CommandArgument.ToString()));
			if (e.CommandName.Equals("OnDelete"))
			{
				this.m_ProductManager.Delete(theProduct);

			}
			else //if (e.CommandName.Equals("OnRecommend") || e.CommandName.Equals("OnFocus"))
			{
                bool theIsExist = true;
                RecommendProduct theRecommendProduct = m_ProductManager.GetRecommendProductByProductId(theProduct.ProductId);
                if (theRecommendProduct == null)
                {
                    theRecommendProduct = new RecommendProduct();
                    theRecommendProduct.RecommendProductId = Guid.NewGuid();
                    theIsExist = false;
                }
                theRecommendProduct.ProductId = theProduct.ProductId;
				theRecommendProduct.CreateTime			= DateTime.Now;
                //新酒
                if (e.CommandName.Equals("OnRecommend"))
                {
                    Session["id"] = e.CommandArgument.ToString();

                    Response.Redirect("xinpro.aspx");
                }
                    //热卖
                else if (e.CommandName.Equals("OnFocus"))
                {
                  Session ["id"]=e.CommandArgument.ToString ();
                  Response.Redirect("hotpro.aspx");
                }
                    //商城推荐
                else if (e.CommandName.Equals("OnPlate"))
                {
                    Session["id"] = e.CommandArgument.ToString();
                    Response.Redirect("RecommendPro.aspx");
                }
                else if (e.CommandName.Equals("OnPoint"))
                {
                    Session["id"] = e.CommandArgument.ToString();
                    Response.Redirect("iProduct.aspx");
                }
                
				//theRecommendProduct.UserId	= this.SignInUser.UserId;
                if (!theIsExist)
                {
                    this.m_ProductManager.AddRecommendProduct(theRecommendProduct);
                }
                else
                {
                    this.m_ProductManager.Update();
                }
			}
		}
	}

	protected void lvProdust_PagePropertiesChanged(object sender, EventArgs e)
	{
		this.BindProduct();
	}




	private void BindProduct()
	{
		List<Product> theProductList	= new List<Product>();
        if (this.txtProductCode.Text.Trim() != string.Empty)
        {
            theProductList = this.m_ProductManager.GetProductByProductCode(this.txtProductCode.Text.Trim());
            this.txtProductCode.Text = string.Empty;
        }

        //else if ("Brand" == this.rblType.SelectedValue )
        //{
        //    if (null != this.ddlSeries.SelectedSeries)
        //    {
        //        theProductList	= this.m_ProductManager.GetProductBySeries(this.ddlSeries.SelectedSeries);
        //    }
        //    else if (null != this.ddlBrand.SelectedBrand)
        //    {
        //        theProductList	= this.m_ProductManager.GetProductByBrand(this.ddlBrand.SelectedBrand);
        //    }
        //}
		else if ("Category" == this.rblType.SelectedValue)
		{

            if (!string.IsNullOrEmpty(ddlL3Category.SelectedValue))
            {
                theProductList = this.m_ProductManager.GetProductByCategory(new Guid(this.ddlL3Category.SelectedValue));
            }
            if(!string.IsNullOrEmpty(ddlL2Category.SelectedValue))
            {
                //theProductList = this.m_ProductManager.GetProductByCategory(new Guid(this.ddlL2Category.SelectedValue));
                theProductList = this.m_ProductManager.GetProductByFirstCategory(new Guid(this.ddlL2Category.SelectedValue));
            }
            //else if (!string.IsNullOrEmpty(ddlL1Category.SelectedValue) && string.IsNullOrEmpty(ddlL2Category.SelectedValue))
            //{
            //    theProductList = this.m_ProductManager.GetProductByFirstCategory(new Guid(this.ddlL1Category.SelectedValue));
            //}
    
		}
		else
		{
            theProductList = this.m_ProductManager.GetAll().OrderByDescending(theProduct => theProduct.CreateTime).ToList(); ;
		}

		this.lvProdust.DataSource = theProductList;
		this.lvProdust.DataBind();
	}


	void SelectedBrandChanged(Brand aBrand)
	{
		
	}

	void SelectedSeriesChanged(ProductSeries aSeries)
	{
		
	}

	protected void rblType_SelectedIndexChanged(object sender, EventArgs e)
	{
		this.pnlBrand.Visible		= ("Brand" == this.rblType.SelectedValue);
        this.pnlCategory.Visible  = ("Category" == this.rblType.SelectedValue);
	}

	protected void btnShow_Click(object sender, EventArgs e)
	{

	}

    //protected void btnSubmit2_Click(object sender, EventArgs e)
    //{
    //    //Category theCategory = m_CategoryManager.GetCategoryById(new Guid(this.hfCategoryId2.Value));
    //    m_IntegralManager.InsertiProduct(this.hfProductId.Value, int.Parse(this.txtSequence.Text.Trim()), decimal.Parse(this.txtPoint.Text.Trim()));
    //    //this.mpeModify2.Hide();
    //    Response.Write("<script>alert('添加成功!')</script>");
    //}
    
}
